<?php

/**
 * ============================================================================
 * smarty的block function(块函数) https://blog.csdn.net/qmhball/article/details/52222847
 * 使用：
  <ul>
  {{hits field="title,url,status" catid="11" limit="10"}}
  <li><a href="{{$r.url}}" title="{{$r.title}}">{{$r.title}}</a></li>
  {{/hits}}
  </ul>
 * ============================================================================ */
$blocksdata = array();

function smarty_block_hits($params, $content, $smarty, &$repeat) {
    global $blocksdata;
    // 对象$smarty上注册一个数组以供block使用
    if (!isset($blocksdata)) {
        $blocksdata = array();
    }
    // 得一个本区块的专属数据存储空间
    $dataindex = substr(md5(__FUNCTION__ . md5(serialize($params))), 0, 16);
    if (!empty($dataindex) && empty($blocksdata[$dataindex])) {// 充数据
        $rs = get_all_hits($params);
        $blocksdata[$dataindex] = $rs;
    }
    // 果没有数据，直接返回null,不必再执行了
    if (!$blocksdata[$dataindex]) {
        $repeat = false;
        return '';
    }
    // 一条数据出栈，并把它指派给$return，重复执行开关置位1
    if (list($key, $item) = each($blocksdata[$dataindex])) {
        $smarty->assign('r', $item);
        $repeat = true;
    }
    // 果已经到达最后，重置数组指针，重复执行开关置位0
    if (!$item) {
        reset($blocksdata[$dataindex]);
        $repeat = false;
    }
    print $content;
}

function get_all_hits($params) {
    $dbprefix = db()->prefix;
    $catid = isset($params['catid']) ? intval($params['catid']) : '';
    if ($catid) {
        $category = get_category($catid);
        if (!$category) {
            return false;
        }
        $arrchildid = $category['arrchildid'];
        $catid = strpos($arrchildid, ',') ? ' AND catid IN (' . $arrchildid . ')' : ' AND catid=' . $arrchildid;
        $modelid = $category['modelid'];
    } else {
        $modelid = isset($params['modelid']) ? $params['modelid'] : '';
        $catid = '';
    }
    $modelinfo = get_modelinfo();
    $tablename = isset($modelinfo[$modelid]['tablename']) ? $modelinfo[$modelid]['tablename'] : 'news';
    $field = isset($params['field']) ? $params['field'] : '*';
    $limit = isset($params['limit']) ? $params['limit'] : '20';
    if (isset($params['where'])) {
        $where = $params['where'];
    } else {
        $thumb = isset($params['thumb']) ? " AND thumb != ''" : '';
        $where = ' status=1 ' . $catid . $thumb;
    }
    //runlog('hits', 'hist:' . $where);
    if ($tablename) {
        $rs = db('content a')
                ->field($field)
                ->join($dbprefix . '' . $tablename . " g on a.contentid=g.contentid")
                ->where($where)
                ->order('views DESC')
                ->limit($limit)
                ->select();
    } else {
        $rs = db('content a')
                ->field($field)
                ->where($where)
                ->order('views DESC')
                ->limit($limit)
                ->select();
    }
    $data = array();
    for ($x = 0; $x <= count($rs); $x++) {
        $row = $rs[$x];
        $row['index'] = $x;
        $data[] = $row;
    }
    return $data;
}
